defold_log("features.cmake:")

option(BUILD_TESTS "Build unit tests" ON)
option(USE_VULKAN "Use Vulkan graphics adapter for app test" OFF)
option(WITH_ASAN "Enable AddressSanitizer (exclusive with other sanitizers)" OFF)
option(WITH_UBSAN "Enable UndefinedBehaviorSanitizer" OFF)
option(WITH_TSAN "Enable ThreadSanitizer" OFF)
option(WITH_VALGRIND "Enable Valgrind integration checks" OFF)

defold_log("BUILD_TESTS: ${BUILD_TESTS}")
defold_log("USE_VULKAN: ${USE_VULKAN}")
defold_log("WITH_ASAN: ${WITH_ASAN}")
defold_log("WITH_UBSAN: ${WITH_UBSAN}")
defold_log("WITH_TSAN: ${WITH_TSAN}")
defold_log("WITH_VALGRIND: ${WITH_VALGRIND}")

set(_DEFOLD_ACTIVE_SANITIZER "")
if(WITH_ASAN)
    set(_DEFOLD_ACTIVE_SANITIZER "ASAN")
elseif(WITH_UBSAN)
    set(_DEFOLD_ACTIVE_SANITIZER "UBSAN")
elseif(WITH_TSAN)
    set(_DEFOLD_ACTIVE_SANITIZER "TSAN")
endif()

if(_DEFOLD_ACTIVE_SANITIZER STREQUAL "ASAN")
    if(MSVC_CL)
        set(_DEFOLD_SAN_COMPILE "/fsanitize=address" "-D_DISABLE_VECTOR_ANNOTATION" "-DDM_SANITIZE_ADDRESS")
        set(_DEFOLD_SAN_LINK "/fsanitize=address")
    else()
        set(_DEFOLD_SAN_COMPILE "-fsanitize=address" "-fno-omit-frame-pointer" "-fsanitize-address-use-after-scope" "-DDM_SANITIZE_ADDRESS")
        set(_DEFOLD_SAN_LINK "-fsanitize=address" "-fno-omit-frame-pointer" "-fsanitize-address-use-after-scope")
    endif()
elseif(_DEFOLD_ACTIVE_SANITIZER STREQUAL "UBSAN")
    set(_DEFOLD_SAN_COMPILE "-fsanitize=undefined" "-DDM_SANITIZE_UNDEFINED")
    set(_DEFOLD_SAN_LINK "-fsanitize=undefined")
elseif(_DEFOLD_ACTIVE_SANITIZER STREQUAL "TSAN")
    set(_DEFOLD_SAN_COMPILE "-fsanitize=thread" "-DDM_SANITIZE_THREAD")
    set(_DEFOLD_SAN_LINK "-fsanitize=thread")
endif()

if(DEFINED _DEFOLD_SAN_COMPILE)
    target_compile_options(defold_sdk INTERFACE ${_DEFOLD_SAN_COMPILE})
    if(DEFINED _DEFOLD_SAN_LINK)
        target_link_options(defold_sdk INTERFACE ${_DEFOLD_SAN_LINK})
    endif()
endif()
